package com.project.controller;

import com.github.pagehelper.PageInfo;
import com.project.bean.ProductBean;
import com.project.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;

@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    private IProductService service;

    @RequestMapping("findByItem")
    public void findByItem(@RequestParam(value = "pageNO",defaultValue = "1") int pageNO,
                           String name,
                           LocalDate startDate,
                           LocalDate endDate,
                           HttpServletRequest request,
                           HttpServletResponse response)throws  Exception{
        //设置请求响应编码集
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String nameStr = name == null ? "":name;
        String startStr = startDate == null ? "":startDate.toString();
        String endStr = endDate == null ? "":endDate.toString();

        //调用业务方法，得到结果
        PageInfo<ProductBean> info = service.findByItem(
                pageNO,name,startDate,endDate);
        String str = "<table border='1' width='60%'>";
        str += "<thead><tr><th>商品名</th><th>单价</th><th>类别</th>" +
                "<th>生产日期</th><th>操作</th></tr></thead>";
        str += "<tbody>";
        for(ProductBean product : info.getList()){
            str += "<tr><td>"+product.getName()+"</td>" +
                    "<td>"+product.getPrice()+"</td>" +
                    "<td>"+product.getType()+"</td>" +
                    "<td>"+product.getCreateDate()+"</td>" +
                    "<td><a href='/product/del?id="+product.getId()+"'>删除</a> " +
                    "<a href='/product/findById?id="+product.getId()+"'>修改</a></td></tr>";
        }

        str += "</tbody></table>";

        for(int i=1;i<=info.getPages();i++){
            str += "<a href='/product/findByItem?pageNO="+i+
                    "&name="+nameStr+"&startDate="+startStr +
                    "&endDate="+endStr+"'>"+i+"</a> ";
        }

        str += "<form action='/product/findByItem'>";

        str += "商品名:<input type='text' name='name' value='"+nameStr+"'><br>";
        str += "起始日期:<input type='text' name='startDate' value='"+startStr+"'><br>";
        str += "结束日期:<input type='text' name='endDate' value='"+endStr+"'><br>";
        str += "<input type='submit' value='查询'>";

        str += "</form>";
        str += "<a href='/html/add.html'>添加</a>";
        response.getWriter().print(str);
    }

    @RequestMapping("add")
    public String add(ProductBean product){
        service.add(product);

        return "redirect:/product/findByItem";
    }

    @RequestMapping("del")
    public String del(int id){
        service.del(id);
        return "redirect:/product/findByItem";
    }

    @RequestMapping("findById")
    public void findById(int id,HttpServletResponse response) throws Exception{
        response.setContentType("text/html;charset=utf-8");
        //按ID得到商品
        ProductBean product = service.findById(id);

        //将商品信息，以form表单方式展示
        String str = "<form action='/product/update'>";
        str += "<input type='hidden' name='id' value='"+id+"'>";
        str += "商品名："+product.getName()+"<br>";
        str += "单价：<input type='text' name='price'" +
                " value='"+product.getPrice()+"'><br>";
        str += "类别："+product.getType()+"<br>";
        str += "生产日期："+product.getCreateDate()+"<br>";
        str += "<input type='submit' value='修改'>";
        str += "</form>";

        response.getWriter().print(str);
    }

    @RequestMapping("update")
    public String update(int id,int price){
        service.update(id,price);
        return "redirect:/product/findByItem";
    }
}
